package com.pansky.crm.controller.system;

import com.pansky.crm.annotation.OperateLog;
import com.pansky.crm.common.mybatisplus.ApiResult;
import com.pansky.crm.common.mybatisplus.ResultPageDto;
import com.pansky.crm.entity.Employee;
import com.pansky.crm.entity.Role;
import com.pansky.crm.entity.bo.EmployeePageBo;
import com.pansky.crm.service.EmployeeService;
import com.pansky.crm.service.RoleService;
import com.pansky.crm.service.impl.RoleEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/2/23 15:43
 * @description：角色controller
 */
@RestController
@RequestMapping("/role")
@OperateLog
@Api(tags = {"管理端-角色api"})
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleEmployeeService roleEmployeeService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "获取所有角色", notes = "获取所有角色")
    @GetMapping("/getAll")
    public ApiResult<List<Role>> getAllRole() {
        return ApiResult.ok(roleService.list());
    }

    @ApiOperation(value = "获取角色成员-员工列表（分页）", notes = "获取角色成员-员工列表（分页）")
    @GetMapping("/pageEmployee")
    public ApiResult<ResultPageDto> listEmployeeByName(@RequestParam Integer roleId
            , @RequestParam(required = false, defaultValue = "1") Integer currentPage
            , @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        EmployeePageBo employeePageBo = new EmployeePageBo();
        employeePageBo.setRoleId(roleId);
        employeePageBo.setPage(currentPage);
        employeePageBo.setLimit(pageSize);
        return ApiResult.ok(employeeService.queryByPage(employeePageBo));
    }

    @ApiOperation(value = "根据角色id获取角色员工列表(无分页)", notes = "根据角色id获取角色员工列表(无分页)")
    @GetMapping("/listAllEmployee")
    public ApiResult<List<Employee>> listAllEmployeeRoleId(@RequestParam Integer roleId) {
        return roleEmployeeService.getAllEmployeeByRoleId(roleId);
    }

    @ApiOperation(value = "添加角色", notes = "添加角色")
    @PostMapping("/add")
    public ApiResult<Boolean> addRole(@Valid @RequestBody Role role) {
        return ApiResult.ok(roleService.save(role));
    }

    @ApiOperation(value = "更新角色", notes = "更新角色")
    @PostMapping("/update")
    public ApiResult<Boolean> updateRole(@Valid @RequestBody Role role) {
        return ApiResult.ok(roleService.updateById(role));
    }

    @ApiOperation(value = "删除角色", notes = "根据id删除角色")
    @GetMapping("/delete")
    public ApiResult<Boolean> deleteRole(@RequestParam Integer id) {
        return ApiResult.ok(roleService.removeById(id));
    }

    @ApiOperation(value = "通过员工id获取角色", notes = "通过员工id获取角色")
    @GetMapping("/getRoles")
    @ApiImplicitParams({@ApiImplicitParam(name = "employeeId", value = "员工id", paramType = "path", required = true)})
    public ApiResult<Role> getRoleNameByEmployeeId(@RequestParam Integer employeeId) {
        Employee employee = employeeService.getById(employeeId);
        if (null != employee.getRoleId()) {
            return ApiResult.ok(roleService.getById(employee.getRoleId()));
        }
        return ApiResult.ok();

    }
}
